package net.zoneland.knowledge.dao;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;

import net.zoneland.knowledge.model.WorkDoneInfo;
import net.zoneland.knowledge.model.WorkDoneStatusInfo;
import net.zoneland.knowledge.model.WorkStatusInfo;

/**
 * 已办状态表数据接口.
 *
 */
@Mapper
public interface WorkDoneStatusInfoMapper {

    /**
     * 插入已办数据.
     *
     * @param info 已办状态记录
     */
    @Insert({
        "insert into aitk_work_done_info ",
        "(ID,WORK_APPLY_ID,OWNER_UID,OWNER_NAME,done_type,CREATE_TIME,UPDATE_TIME)",
        "values (#{id,jdbcType=VARCHAR}, #{workApplyId,jdbcType=VARCHAR}, #{ownerUid,jdbcType=VARCHAR}, ",
        "#{ownerName,jdbcType=VARCHAR}, ",
        "#{doneType,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, ",
        "#{updateTime,jdbcType=TIMESTAMP})"
    })
    void insertDoneInfo(final WorkDoneStatusInfo info);

    /**
     * 插入状态数据.
     *
     * @param info 已办状态记录
     */
    @Insert({
        "insert into aitk_work_status_info ",
        "(ID,WORK_APPLY_ID,CURRENT_STATUS,CURRENT_HANDLER_UID,CURRENT_HANDLER_NAME,CREATE_TIME,UPDATE_TIME)",
        "values (#{id,jdbcType=VARCHAR}, #{workApplyId,jdbcType=VARCHAR}, #{currentStatus,jdbcType=VARCHAR}, ",
        "#{currentHandlerUid,jdbcType=VARCHAR}, #{currentHandlerName,jdbcType=VARCHAR}, ",
        "#{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP})"
    })
    void insertStatusInfo(final WorkDoneStatusInfo info);

    /**
     * 更新已办状态信息.
     *
     * @param info 已办状态记录
     */
    @Update({
        "update aitk_work_status_info set CURRENT_STATUS=#{currentStatus,jdbcType=VARCHAR},",
        "CURRENT_HANDLER_UID=#{currentHandlerUid,jdbcType=VARCHAR}, CURRENT_HANDLER_NAME=#{currentHandlerName,jdbcType=VARCHAR},",
        "UPDATE_TIME=#{updateTime,jdbcType=TIMESTAMP}",
        "where WORK_APPLY_ID=#{workApplyId,jdbcType=VARCHAR}"
    })
    void updateStatusInfo(final WorkDoneStatusInfo info);

    /**
     * 查询已办状态记录.
     * @param workDoneStatusInfo 查询条件
     * @return 已办状态记录列表
     */
    @Select({
        "<script>",
        "select ID,WORK_APPLY_ID,OWNER_UID,OWNER_NAME,",
        "done_type,CREATE_TIME,UPDATE_TIME",
        "FROM aitk_work_done_info ",
        "WHERE 1=1",
        "<if test=\"id != null and id != ''\"> AND ID=#{id,jdbcType=VARCHAR} </if> ",
        "<if test=\"workApplyId != null and workApplyId != ''\"> AND WORK_APPLY_ID=#{workApplyId,jdbcType=VARCHAR} </if> ",
        "<if test=\"ownerUid != null and ownerUid != ''\"> AND OWNER_UID=#{ownerUid,jdbcType=VARCHAR} </if> ",
        "</script>"
    })
    @Results(id = "WorkDoneInfoResultMap", value = {
        @Result(column = "ID", property = "id", jdbcType = JdbcType.VARCHAR, id = true),
        @Result(column = "WORK_APPLY_ID", property = "workApplyId", jdbcType = JdbcType.VARCHAR),
        @Result(column = "OWNER_UID", property = "ownerUid", jdbcType = JdbcType.VARCHAR),
        @Result(column = "OWNER_NAME", property = "ownerName", jdbcType = JdbcType.VARCHAR),
        @Result(column = "done_type", property = "doneType", jdbcType = JdbcType.VARCHAR),
        @Result(column = "CREATE_TIME", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
        @Result(column = "UPDATE_TIME", property = "updateTime", jdbcType = JdbcType.TIMESTAMP)
    })
    List<WorkDoneInfo> queryDoneInfo(final WorkDoneStatusInfo workDoneStatusInfo);

    /**
     * 查询已办状态记录.
     * @param workDoneStatusInfo 查询条件
     * @return 已办状态记录列表
     */
    @Select({
        "<script>",
        "select ID,WORK_APPLY_ID,CURRENT_STATUS,",
        "CURRENT_HANDLER_UID,CURRENT_HANDLER_NAME,CREATE_TIME,UPDATE_TIME",
        "FROM aitk_work_status_info ",
        "WHERE 1=1",
        "<if test=\"id != null and id != ''\"> AND ID=#{id,jdbcType=VARCHAR} </if> ",
        "<if test=\"workApplyId != null and workApplyId != ''\"> AND WORK_APPLY_ID=#{workApplyId,jdbcType=VARCHAR} </if> ",
        "</script>"
    })
    @Results(id = "WorkStatusInfoResultMap", value = {
        @Result(column = "ID", property = "id", jdbcType = JdbcType.VARCHAR, id = true),
        @Result(column = "WORK_APPLY_ID", property = "workApplyId", jdbcType = JdbcType.VARCHAR),
        @Result(column = "CURRENT_STATUS", property = "currentStatus", jdbcType = JdbcType.VARCHAR),
        @Result(column = "CURRENT_HANDLER_UID", property = "currentHandlerUid", jdbcType = JdbcType.VARCHAR),
        @Result(column = "CURRENT_HANDLER_NAME", property = "currentHandlerName", jdbcType = JdbcType.VARCHAR),
        @Result(column = "CREATE_TIME", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
        @Result(column = "UPDATE_TIME", property = "updateTime", jdbcType = JdbcType.TIMESTAMP)
    })
    List<WorkStatusInfo> queryStatusInfo(final WorkDoneStatusInfo workDoneStatusInfo);

    /**
     * 根据工单id获取状态信息数据
     * @param workApplyId 工单id
     */
    @Select({
            " select ID,WORK_APPLY_ID,CURRENT_STATUS,CURRENT_HANDLER_UID,CURRENT_HANDLER_NAME,CREATE_TIME,UPDATE_TIME ",
            " from aitk_work_status_info ",
            " where WORK_APPLY_ID=#{workApplyId,jdbcType=VARCHAR} and ROWNUM = 1 "
    })
    @ResultMap(value = {"WorkStatusInfoResultMap"})
    WorkStatusInfo getWorkStatusInfoByWorkApplyId(final String workApplyId);

    /**
     * 更新状态信息数据
     * @param info 已办状态记录
     */
    @Update({
            " update aitk_work_status_info ",
            " set CURRENT_STATUS=#{currentStatus,jdbcType=VARCHAR},CURRENT_HANDLER_UID=#{currentHandlerUid,jdbcType=VARCHAR}, ",
            " CURRENT_HANDLER_NAME=#{currentHandlerName,jdbcType=VARCHAR},UPDATE_TIME=#{updateTime,jdbcType=TIMESTAMP} ",
            " where WORK_APPLY_ID=#{workApplyId,jdbcType=VARCHAR} "
    })
    void updateWorkStatusInfo(final WorkStatusInfo info);

}
